package com.junlong0716.enjoylearning.widget

import android.content.Context
import android.content.Intent
import android.graphics.PorterDuff
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ToastUtils
import com.htxcsoft.corelibrary.glide.GlideUtils
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.service.PlayMusicCallback
import com.junlong0716.enjoylearning.service.PlayerManager
import com.junlong0716.enjoylearning.ui.AudioActivity
import com.wang.avi.AVLoadingIndicatorView
import de.hdodenhof.circleimageview.CircleImageView
import java.text.SimpleDateFormat

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午10:58 2018/7/31
 *@modified by:
 */
class PlayerBarView : PlayMusicCallback {
    private var mContext: Context? = null
    private var mPlayerManager: PlayerManager? = null
    private var mRootView: ViewGroup? = null
    private var mAboveView: Int? = null

    private lateinit var audioView: View
    private lateinit var tvAudioDuration: TextView
    private lateinit var tvAudioTitle: TextView
    private lateinit var ivPlayOrPause: ImageView
    private lateinit var ivAudioAvatar: ImageView
    private var playPauseDrawable: PlayPauseDrawable? = null
    private var isPlaying = false
    private val time = SimpleDateFormat("mm:ss")
    private lateinit var ivLoadingView: AVLoadingIndicatorView
    private lateinit var ivLoadingShadow: CircleImageView

    constructor(context: Context, playerManager: PlayerManager, rootView: ViewGroup) {
        this.mContext = context
        this.mPlayerManager = playerManager
        this.mRootView = rootView
        mPlayerManager!!.addListenerCallback(this)
        initPlayerBar()
    }

    constructor(context: Context, playerManager: PlayerManager, rootView: ViewGroup, aboveView: Int) {
        this.mAboveView = aboveView
        this.mContext = context
        this.mPlayerManager = playerManager
        this.mRootView = rootView
        mPlayerManager!!.addListenerCallback(this)
        initPlayerBar()
    }


    private fun initPlayerBar() {
        playPauseDrawable = PlayPauseDrawable(mContext!!, mContext!!.resources.getDimensionPixelSize(R.dimen.pause_bar_width).toFloat(), mContext!!.resources.getDimensionPixelSize(R.dimen.pause_bar_height).toFloat(), mContext!!.resources.getDimensionPixelSize(R.dimen.pause_bar_distance).toFloat())
        playPauseDrawable!!.setColorFilter(ContextCompat.getColor(mContext!!, R.color.common_transparent100_white), PorterDuff.Mode.SRC_IN)
        audioView = LayoutInflater.from(mContext).inflate(R.layout.view_audio_play_bar_layout, null)
        ivPlayOrPause = audioView.findViewById<ImageView>(R.id.iv_play_pause)
        ivAudioAvatar = audioView.findViewById<ImageView>(R.id.iv_audio_avatar)
        ivLoadingShadow = audioView.findViewById<CircleImageView>(R.id.iv_loading_shadow)
        ivLoadingView = audioView.findViewById<AVLoadingIndicatorView>(R.id.loadingView)
        ivPlayOrPause.setImageDrawable(playPauseDrawable)
        val lp = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ConvertUtils.dp2px(50f))
        if (mAboveView != null) {
            lp.addRule(RelativeLayout.ABOVE, mAboveView!!)
        }else{
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        }
        lp.leftMargin = ConvertUtils.dp2px(16f)
        lp.rightMargin = ConvertUtils.dp2px(16f)
        lp.bottomMargin = ConvertUtils.dp2px(5f)
        audioView.layoutParams = lp
        mRootView!!.addView(audioView)
        audioView.setOnClickListener {
            mContext!!.startActivity(Intent(mContext, AudioActivity::class.java))
            //overridePendingTransition(R.anim.anim_up, 0)
        }
        tvAudioDuration = audioView.findViewById(R.id.tv_audio_duration)
        tvAudioTitle = audioView.findViewById(R.id.tv_audio_title)

        if (mPlayerManager!!.getPlayerBarVisibilityState()) {
            audioView.visibility = View.VISIBLE
            val audioInfo = mPlayerManager!!.getAudioInfo()
            tvAudioTitle.text = audioInfo!!.name
            GlideUtils.showImage(mContext!!, audioInfo.img, ivAudioAvatar)
            if (mPlayerManager!!.getCurrentMediaPlayerState()) {
                playPauseDrawable!!.setPause(true)
            }else{
                playPauseDrawable!!.setPlay(true)
            }
        }else{
            audioView.visibility = View.GONE
        }

        ivPlayOrPause.setOnClickListener {
            if (isPlaying) {
                mPlayerManager!!.pauseMusic()
            } else {
                mPlayerManager!!.playMusic()
            }
        }

        val ivCloseAudioBar = audioView.findViewById<ImageView>(R.id.iv_close)

        ivCloseAudioBar.setOnClickListener {
            mPlayerManager!!.stopMusic()
        }
    }

    fun setPlayerBarVisibility(visibility: Boolean) {
        if (visibility) audioView.visibility = View.VISIBLE else audioView.visibility = View.GONE
    }

    override fun onPlayMusicComplete() {
        isPlaying = false
        playPauseDrawable!!.setPlay(true)
    }

    override fun onPlayerBarVisibleState(isVisible: Boolean) {

    }

    override fun onPlayerMediaPrepared() {

        ivLoadingShadow.visibility = View.GONE
        ivLoadingView.visibility = View.GONE
    }

    override fun onPlayerCurrentPosition(currentPosition: Int) {
        tvAudioDuration.text = time.format(currentPosition)

        if (mPlayerManager!!.getCurrentMediaPlayerState()) {
            playPauseDrawable!!.setPause(true)
        } else {
            playPauseDrawable!!.setPlay(true)
        }
    }

    override fun onPlayerStart(isFirstLoading: Boolean) {
        if (isFirstLoading) {
            ivLoadingShadow.visibility = View.VISIBLE
            ivLoadingView.visibility = View.VISIBLE
        }
        mPlayerManager!!.setPlayerBarVisibilityState(true)
        isPlaying = true
        val audioInfo = mPlayerManager!!.getAudioInfo()
        tvAudioTitle.text = audioInfo!!.name
        GlideUtils.showImage(mContext!!.applicationContext, audioInfo.img, ivAudioAvatar)
        setPlayerBarVisibility(true)
        playPauseDrawable!!.setPause(true)
    }

    override fun onPlayerPause() {
        isPlaying = false
        playPauseDrawable!!.setPlay(true)
    }

    override fun onPlayerStop() {
        mPlayerManager!!.setPlayerBarVisibilityState(false)
        setPlayerBarVisibility(false)
    }
}